﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgramShortcuts.Components.Services;

namespace ProgramShortcuts.Components.Functions
{
	public class AlarmFunctionState : IFunctionState
	{		
		IClearScreen _ClearScreen;
		IWriteString _WriteString;
		StringBuilder _StringBuilder;
		public AlarmFunctionState(IClearScreen clearScreen,IWriteString writerString)
		{
			_ClearScreen = clearScreen;
			_WriteString = writerString;
			_StringBuilder = new StringBuilder();
		}
		public void InteractWithUser( ConsoleKeyInfo consoleKeyInfo )
		{
			if ( consoleKeyInfo.Key == ConsoleKey.Backspace )
			{
				_WriteString.Write( "\b \b" );// moves the cursor back one position
												// overrides character at position with space
												// moves the cursor back one position again
			}
			if ( _StringBuilder.Length > 0 && consoleKeyInfo.Key == ConsoleKey.Enter )
			{
				System.Windows.Forms.MessageBox.Show( _StringBuilder.ToString() );
				_StringBuilder = new StringBuilder();

				_WriteString.Write( Environment.NewLine );
				InformUserOfCurrentState();
				return;
			}
			if (Char.IsWhiteSpace(consoleKeyInfo.KeyChar) 
				|| Char.IsLetterOrDigit( consoleKeyInfo.KeyChar ) 
				|| Char.IsPunctuation( consoleKeyInfo.KeyChar ) )
			{
				_WriteString.Write( consoleKeyInfo.KeyChar.ToString() );
				_StringBuilder.Append( consoleKeyInfo.KeyChar );
			}

		}

		#region IExecute Members

		public void InformUserOfCurrentState()
		{
			_ClearScreen.ClearCurrentLine();
			_WriteString.Write( "Alarm " );
		}

		#endregion
	}
}
